package edu.friday.repository;

import edu.friday.model.SysRoleEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 角色表数据层
 */
@Repository
public interface SysRoleRepository extends JpaRepository<SysRoleEntity, Long> {

    @Query(value = "select r.role_id from sys_role r" +
            " left join sys_user_role ur on ur.role_id = r.role_id" +
            " left join sys_user u on u.user_id = ur.user_id" +
            " where r.del_flag = '0' and u.user_id = :userId ", nativeQuery = true)
    List<Long> selectRoleIdsByUserId(@Param("userId") Long userId);

    @Query(value = "select distinct r.* from sys_role r" +
            " left join sys_user_role ur on ur.role_id = r.role_id" +
            " left join sys_user u on u.user_id = ur.user_id" +
            " where r.del_flag = '0' and u.user_id = :userId", nativeQuery = true)
    List<SysRoleEntity> selectRoleByUserId(@Param("userId") Long userId);
}
